package com.mouse.hxsj.service;

import com.mouse.damo.micro.*;
import com.mouse.haoi.entity.Point;
import com.mouse.haoi.service.CustomService;
import com.mouse.hxsj.constants.SystemParams;
import com.mouse.hxsj.entity.common.GameMap;
import com.mouse.hxsj.entity.enums.MapPlace;
import com.mouse.hxsj.util.CommonUtil;
import com.mouse.hxsj.util.StringUtil;
import lombok.Setter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class FamlilyService {
    static CustomService customService = new CustomService();
    static boolean untilClick;
    static GameMap gameMap2;

    public static void main(String[] args) {
        Com com = new Com();
        com.setResourcePath("D:\\damo");
        boolean bind = com.bind(919318);
        System.out.println(bind);
        com.setDict(7, "家族领地.txt");
//        FindStr findStr = new FindStr(com);
//        boolean inJzFlag = customService.loop(120, () -> findStr.findStrE(0, 0, 1000, 800,
//                "家族完成", "30e926-000000", 0.9, 1)[0] != -1);
//        System.out.println(inJzFlag);
//        if(true)return;
        //
        for (int i = 0; i < 10; i++) {
//            start(com, false);
        }
    }

    @Setter
    private Com com;
    @Setter
    private boolean lingdi;
    @Setter
    private boolean open = false;

    public void start1() {
        while (true) {
            Sleep.sleep(50);
            if (!open) continue;
            try {
                start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void start() {
        FindPic findPic = new FindPic(com);
        FindStr findStr = new FindStr(com);
        Window window = new Window(com);

//        if(true)return;
        Mouse mouse = new Mouse(com);
        Press press = new Press(com);
        GameMap gameMap = CommonUtil.getMap(com);

        //
        int flyPress = Press.F4;
        String mapStr0 = "家族";
        if (lingdi) {
            flyPress = Press.F5;
            mapStr0 = "领地";
        }
        int flyPressFinal = flyPress;
        press.keyPress(flyPressFinal);
        String finalMap = mapStr0;
        int[] windowRect = window.getClientSize(com.getHwnd());
        int height = windowRect[2], width = windowRect[1];
        customService.loop(3, () -> {
            Sleep.sleep(100);
            int[] strE = findStr.findStrE(0, 0, width, height,
                    "准备传送", "ffffff-000000", 0.9, 7);
            if (strE[0] != -1) {
                return true;
            }
            press.keyPress(flyPressFinal);
            return false;
        });

        boolean inJzFlag = customService.loop(10, () -> CommonUtil.getMap(com).getMap().contains(finalMap));
        System.out.println(inJzFlag);
        press.keyPress(Press.M);
        Sleep.sleep(300);
        List<Integer> pointList = new ArrayList<>();

        boolean mFlag = customService.loop(5, () -> {
            int[] pic = findPic.findPic(0, 0, width, height,
                    "login/场景地图.bmp", "203040", 0.8, 0);
            if (pic[0] != -1) pointList.addAll(Arrays.asList(pic[1], pic[2]));
            return pic[0] != -1;
        });
        System.out.println(mFlag);
        if (lingdi) {
            mouse.mouseDoubleClick(pointList.get(0) + 374, pointList.get(1) + 45 + 58);
        } else {
            mouse.mouseDoubleClick(pointList.get(0) + 374, pointList.get(1) + 45);
        }
        Sleep.sleep(100);
        press.keyPress(Press.M);
        //点击NPC
        pointList.clear();
        //完成任务
        boolean npcFlag = customService.loop(10, () -> {
            int[] pic = findPic.findPic(0, 0, width, height,
                    "login/家族头像.bmp|login/领地头像.bmp", "203040", 0.8, 0);
            if (pic[0] != -1) pointList.addAll(Arrays.asList(pic[1], pic[2]));
            return pic[0] != -1;
        });
        Sleep.sleep(500);
        wanJie(mouse, pointList);
        //判断任务是否完成
        //
        Sleep.sleep(1000);
        //查看任务详情
        int[] strE = findStr.findStrE(0, 0, width, height, "家族完成|领地完成", "fff2b6-000000", 0.9, 7);
        if (strE[0] == -1) return;
        mouse.mouseMoveTo(strE[1] + 20, strE[2] + 0);
        Sleep.sleep(100);
        mouse.mouseMoveTo(strE[1] + 30, strE[2] + 5);
        Sleep.sleep(100);
        mouse.mouseClick(true);
        Sleep.sleep(1000);
        String mapStr = findStr.readStr(strE[1] - 300, 0, strE[1] + 10, height,
                "ffffff-000000", "", 0.9, 7);
        System.out.println(mapStr + "---");
        if (StringUtil.isBlank(mapStr)) return;
        mapStr = mapStr.replace("对话", "");
        if (StringUtil.isBlank(mapStr)) return;
        press.keyPress(Press.ESC);
        if (StringUtil.isBlank(mapStr) && findStr.findStrE(0, 0, width, height,
                "家族完成|领地完成", "30e926-000000", 0.9, 7)[0] != -1) {
            return;
        }
        if (StringUtil.isNotBlank(mapStr.replace("永乐城", ""))) mapStr = mapStr.replace("永乐城", "");
        MapPlace mapPlace = MapPlace.getByMap(mapStr);
        MapPlace.fly(com, mapPlace);
        String mapStrFinal = mapStr;
        inJzFlag = customService.loop(10, () -> CommonUtil.getMap(com).getMap().equals(mapStrFinal));
        System.out.println(inJzFlag);
        //点击任务
        mouse.mouseMoveTo(strE[1] + 25, strE[2] + 20);
        Sleep.sleep(100);
        mouse.mouseMoveTo(strE[1] + 30, strE[2] + 22);
        Sleep.sleep(100);
        mouse.mouseClick(true);
        Sleep.sleep(500);
        mouse.mouseDoubleClick(60, 40);
        gameMap2 = CommonUtil.getMap(com);
        untilClick = true;
        long t1 = System.currentTimeMillis();
        inJzFlag = customService.loop(180, () -> {
            //无法对话
            if (System.currentTimeMillis() - t1 > 5000 && untilClick) {
                GameMap gameMap3 = CommonUtil.getMap(com);
                if (StringUtil.isBlank(gameMap3.getMap()) || gameMap3.getX() == -1) {
                } else if (gameMap3.getMap().equals(gameMap2.getMap())
                        && gameMap3.getX() == gameMap2.getX() && gameMap3.getY() == gameMap2.getY()) {
                    mouse.mouseDoubleClick(60, 40);
                    Sleep.sleep(500);
                    mouse.mouseClick(strE[1] + 30, strE[2] + 22, true);
                    untilClick = false;
                } else {
                    gameMap2 = gameMap3;
                }
            }
            //对话
            int[] duihua = findStr.findStrE(0, 0, width, height,
                    "对话", "000000-000000", 0.9, 7);
            if (duihua[0] != -1) {
                mouse.mouseClick(duihua[1], duihua[2] + 2, true);
                Sleep.sleep(300);
                int[] duihua2 = findStr.findStrE(0, 0, width, height,
                        "对话完成", "000000-000000", 0.9, 7);
                mouse.mouseClick(duihua2[1], duihua2[2] + 2, true);
                Sleep.sleep(300);
            }
            int[] wan = findStr.findStrE(0, 0, width, height,
                    "家族完成|领地完成", "30e926-000000", 0.9, 7);
            if (wan[0] != -1) {
                mouse.mouseClick(500, 400, true);
                return true;
            }
            return false;
        });
    }

    private static void wanJie(Mouse mouse, List<Integer> pointList) {
        FindPic findPic = new FindPic(mouse.getCom());
        jieWan(mouse, pointList);
        //接任务
        pointList.clear();
        boolean npcFlag = customService.loop(3, () -> {
            int[] pic = findPic.findPic(0, 0, 961, 187,
                    "login/家族头像.bmp|login/领地头像.bmp", "203040", 0.8, 0);
            if (pic[0] != -1) pointList.addAll(Arrays.asList(pic[1], pic[2]));
            return pic[0] != -1;
        });
        if (npcFlag) {
            Sleep.sleep(500);
            jieWan(mouse, pointList);
        }
    }

    private static void jieWan(Mouse mouse, List<Integer> pointList) {
        Com otherCom = new Com();
        otherCom.setResourcePath(SystemParams.sourcePath);
        otherCom.setDict(7, "家族领地.txt");
        FindPic otherFindPic = new FindPic(otherCom);
        Mouse otherMouse = new Mouse(otherCom);
        SystemUtil systemUtil = new SystemUtil(mouse.getCom());
        int wheight = systemUtil.getScreenHeight(), wwidth = systemUtil.getScreenWidth();
        FindStr otherFindStr = new FindStr(otherCom);


        int[] mousePoint = mouse.getMousePoint();
        Com com = mouse.getCom();
        Window window1 = new Window(com);
        int[] windowRect = window1.getClientSize(com.getHwnd());
        int height = windowRect[2], width = windowRect[1];
//        Com com = mouse.getCom();
        mouse.mouseDoubleClick(pointList.get(0) + 54, pointList.get(1) + 212);
        Sleep.sleep(700);
        //提交物品
        FindStr findStr = new FindStr(mouse.getActive());
        FindPic findPic = new FindPic(mouse.getCom());
        int[] wupin = findStr.findStrE(0, 0, width, height,
                "物品", "000000-000000", 0.9, 7);
        if (wupin[0] != -1) {
            Sleep.sleep(200);
            Point loopPoint = customService.loopPoint(10, () -> {
                int[] pic = findPic.findPic(0, 0, width, height,
                        "login/提交物品.bmp", "203040", 0.9, 0);
                return new Point(pic);
            });
            Sleep.sleep(200);
            if (loopPoint.getState() != -1) {
                Window window = new Window(mouse.getCom());
                window.setWindowActivate(mouse.getCom().getHwnd());
                Point point = customService.loopPoint(10, () -> {
                    int[] pic = otherFindPic.findPic(0, 0, wwidth, wheight,
                            "login/提交物品.bmp", "203040", 0.9, 0);
                    return new Point(pic);
                });
                int[] pic = otherFindStr.findStrE(0, 0, width, height,
                        "物品", "000000-000000", 0.9, 7);
                if (point.getState() != -1 && pic[0] != -1) {
                    int x = point.getX() + 20, y = point.getY() + 20;
                    Sleep.sleep(50);
                    otherMouse.mouseMoveTo(x, y);
                    Sleep.sleep(50);
                    otherMouse.mouseClick(x, y, true);
                    Sleep.sleep(500);
                    otherMouse.mouseClick(x, y, true);
                    Sleep.sleep(500);
                    otherMouse.leftDown();
                    Sleep.sleep(10);
                    otherMouse.mouseMoveTo(pic[1] - 10, pic[2] + 50);
                    Sleep.sleep(200);
                    otherMouse.leftUp();
                    Sleep.sleep(200);
                    otherMouse.mouseClick(pic[1] - 10, pic[2] + 50, true);
                }
//                boolean loop = customService.loop(20, () -> {
//                    int[] strE = findStr.findStrE(0, 0, width, height,
//                            "家族完成|领地完成", "fff2b6-000000", 0.9, 7);
//                    return strE[0] != -1;
//                });
//                if (loop) {
//                    mouse.mouseDoubleClick(pointList.get(0) + 229, pointList.get(1) + 426);
//                    return;
//                }
            }
        }
        int[] pic = findPic.findPic(0, 0, width, height,
                "login/家族头像.bmp|login/领地头像.bmp", "203040", 0.8, 0);
        Sleep.sleep(200);
        if (pic[0] != -1) {
            mouse.mouseDoubleClick(pointList.get(0) + 229, pointList.get(1) + 426);
        }
        int[] pic2 = findPic.findPic(0, 0, width, height,
                "login/关闭背包.bmp", "203040", 0.9, 0);
        if (pic2[0] != -1) {
            mouse.mouseClick(pic2[1] + 126, pic2[2] - 10, true);
            Sleep.sleep(100);
        }
    }

    private static void moveFood(Mouse mouse, List<Integer> pointList) {
        mouse.mouseDoubleClick(pointList.get(0) + 54, pointList.get(1) + 212);
        Sleep.sleep(500);
        mouse.mouseDoubleClick(pointList.get(0) + 229, pointList.get(1) + 426);
    }
}
